/**
 * 收费计划
 */

<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <div class="courseBoxTopSearch">
        <div class="courseName">
          <span>收费名称：</span>
          <el-input
            v-model="formSearch.chargeName"
            placeholder="请输入收费名称"
          />
        </div>
        <div class="pictureVersion">
          <span>应用学年：</span>
          <div class="block">
            <el-date-picker
              v-model="formSearch.yearStartTime"
              align="right"
              type="date"
              placeholder="开始时间"
            >
            </el-date-picker>
            <span>-</span>
            <el-date-picker
              v-model="formSearch.yearEndTime"
              align="right"
              type="date"
              placeholder="结束时间"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="courseName">
          <span>收费项目：</span>
          <el-input
            v-model="formSearch.chargeProject"
            placeholder="请输入收费项目"
          />
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="searchInput">
            <i class="el-icon-search"></i>
            <span>搜索</span>
          </el-button>
          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>
    </div>

    <div class="courseBoxBottom">
      <div class="bottomHeader">
        <span class="title">管理列表</span>
        <div class="bottomHeaderBtn">
          <el-button class="addBtn" @click="openAddPicture">
            <i class="el-icon-plus"></i>
            <span>新增</span>
          </el-button>
          <el-button class="delBtn" @click="handleBatchDel">
            <i class="el-icon-close"></i>
            <span>批量删除</span>
          </el-button>
        </div>
      </div>
      <div class="bottomMian">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="chargeName" label="收费名称">
          </el-table-column>
          <el-table-column prop="yearStartTime" label="应用学年" width="180">
            <template slot-scope="scope">
              {{ scope.row.yearStartTime }}
              <span v-if="scope.row.yearStartTime && scope.row.yearEndTime"
                >年 ~</span
              >
              {{ scope.row.yearEndTime }}
              <span v-if="scope.row.yearStartTime && scope.row.yearEndTime"
                >年</span
              >
            </template>
          </el-table-column>
          <el-table-column prop="collegeName" label="应用对象" width="280">
            <template slot-scope="scope">
              <span style="font-size: 12px">
                {{ scope.row.collegeName }}
                {{ scope.row.majorName }}
                {{ scope.row.gradeName }}
                {{ scope.row.className }}
                {{ scope.row.studentName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="收费项目" prop="chargeProject">
            <template slot-scope="scope">
              <p v-for="(item, index) in scope.row.chargeProject" :key="index">
                {{ item.chargeItem }}
              </p>
            </template>
          </el-table-column>
          <el-table-column label="收费金额(元)" prop="chargeProject">
            <template slot-scope="scope">
              <p v-for="(item, index) in scope.row.chargeProject" :key="index">
                {{ item.money }}
              </p>
            </template>
          </el-table-column>
          <el-table-column label="收费小计(元)" prop="amount">
          </el-table-column>
          <el-table-column prop="createTime" label="缴费时间" width="300">
            <template slot-scope="scope">
              {{ scope.row.paymentStartTime }}
              <span
                v-if="scope.row.paymentStartTime && scope.row.paymentEndTime"
                >~</span
              >
              {{ scope.row.paymentEndTime }}
            </template>
          </el-table-column>
          <el-table-column label="自动续费">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.automaticFeeDeduction"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                @change="updateAutomaticFeeDeduction(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <div class="action">
                <span
                  type="text"
                  v-if="scope.row.state == '1'"
                  @click="updateIsUsing(scope.row, '0')"
                >
                  已发布
                </span>
                <span
                  type="text"
                  class="copy"
                  v-else
                  @click="updateIsUsing(scope.row, '1')"
                >
                  发布
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <div class="action">
                <div
                  class="icon edit"
                  @click="handleEdit(scope.row.id)"
                >编辑</div>
                <div
                  class="icon del"
                  @click="handleDel(scope.row.id)"
                >删除</div>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import moment from "moment";
import { _debounce } from "@/utils/debounce";
import {
  delChargeplanApi,
  editChargeplanApi,
  getChargeplanListApi,
} from "@/api/finance/chargeplan";

export default {
  data() {
    return {
      formSearch: {
        chargeName: "",
        chargeProject: "",
        yearStartTime: "",
        yearEndTime: "",
      },
      tableData: [],
      total: 0,
      multipleSelection: [],
      currentPage: 1,
      pageSize: 10,
    };
  },

  created() {
    // 获取列表
    this.getChargeplanList({
      pageNum: this.currentPage,
      pageSize: this.pageSize,
    });
  },

  methods: {
    // 获取列表
    async getChargeplanList(data) {
      let res = await getChargeplanListApi(data);
      if (res.code === 200) {
        this.tableData = res.rows.map((item) => {
          return {
            ...item,
            automaticFeeDeduction:
              item.automaticFeeDeduction == "1" ? true : false,
            chargeProject:
              item.chargeProject && item.chargeProject != "null"
                ? JSON.parse(item.chargeProject)
                : [],
          };
        });
        this.total = res.total;
      }
    },

    // 搜索
    searchInput: _debounce(function () {
      this.formSearch.yearStartTime = this.formSearch.yearStartTime
        ? moment(this.formSearch.yearStartTime).format("YYYY")
        : "";
      this.formSearch.yearEndTime = this.formSearch.yearEndTime
        ? moment(this.formSearch.yearEndTime).format("YYYY")
        : "";
      // 获取列表
      this.getChargeplanList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.formSearch,
      });
    }, 200),

    // 重置
    refreshInput: _debounce(function () {
      this.formSearch = {
        chargeName: "",
        chargeProject: "",
        yearStartTime: "",
        yearEndTime: "",
      };
      // 获取列表
      this.getChargeplanList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    }, 200),

    // 新增
    openAddPicture() {
      this.$router.push("/finance/addcharge");
    },

    // 编辑
    handleEdit(id) {
      this.$router.push({
        path: "/finance/addcharge",
        query: { id },
      });
    },

    // 删除
    handleDel(ids) {
      this.$confirm("确定删除该数据吗? 删除后将不可恢复", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delChargeplanApi({ ids }).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功！");
            // 获取列表
            this.getChargeplanList({
              pageNum: this.currentPage,
              pageSize: this.pageSize,
              ...this.formSearch,
            });
          } else {
            this.$message.error("删除失败！");
          }
        });
      });
    },

    // 批量删除
    handleBatchDel() {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.id;
        });

        this.$confirm("确定删除该数据吗? 删除后将不可恢复", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delChargeplanApi({ ids: data.join(",") }).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功！");
              // 获取列表
              this.getChargeplanList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
                ...this.formSearch,
              });
            } else {
              this.$message.error("删除失败！");
            }
          });
        });
      } else {
        this.$message.error("请选择要删除的数据！");
      }
    },

    // 修改发布状态
    updateIsUsing(data, state) {
      editChargeplanApi({ id: data.id, state }).then((res) => {
        if (res.code === 200) {
          this.$message.success("修改成功！");
          // 获取列表
          this.getChargeplanList({
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            ...this.formSearch,
          });
        } else {
          this.$message.error("修改失败！");
        }
      });
    },

    // 是否自动缴费
    updateAutomaticFeeDeduction(data) {
      let automaticFeeDeduction = data.automaticFeeDeduction ? "1" : "0";
      editChargeplanApi({ id: data.id, automaticFeeDeduction }).then((res) => {
        if (res.code === 200) {
          this.$message.success("修改成功！");
          // 获取列表
          this.getChargeplanList({
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            ...this.formSearch,
          });
        } else {
          this.$message.error("修改失败！");
        }
      });
    },

    // 已选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 设置当前页码
    handleCurrentChange(val) {
      this.currentPage = val;
      // 获取列表
      this.getChargeplanList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.formSearch,
      });
    },

    // 跳转指定页码
    goPage() {
      let { value } = this.$refs.currentPage;
      this.currentPage = Number(value);
      // 获取列表
      this.getChargeplanList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.formSearch,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }
}

::v-deep .el-icon-date {
  line-height: 36px;
}

.copy {
  color: #4b93d5;
  cursor: pointer;
}

::v-deep .el-table td.el-table__cell .action {
  display: flex;
  // width: 50px;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.courseName,
.profession {
  ::v-deep .el-input__inner {
    width: 200px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 200px;
  }
}

.pictureVersion {
  ::v-deep .el-input__inner {
    width: 140px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 140px;
  }
}
.del {
  margin-left: 10px;
  // font-weight: bold;
  color: #1990FF;
  cursor: pointer;
}
.edit{
  // font-weight: bold;
  // width: 50px;
  margin-right: 10px;
  color: #1990FF;
  cursor: pointer;
}
</style>

